<?php
namespace app\shop\controller\admin;

use app\admin\library\AdminController;

class Spec extends AdminController{

    public function onIndex(){

        $this->pageTitle = '规格管理';
        $this->pageIcon = 'fa-file-text-o';
        $this->power('shop.setting.spec');
        return $this->display();
    }

    public function onList(){
        $this->power('shop.setting.spec');
        $keyword =  $this->post('keyword',$this->check()->type('string'));
        $pageCurrent = $this->post('page_current',$this->check('当前页')->default(1)->type('int'));
        $spec = $this->model('Spec');
        
        if ($keyword){
            $spec->where('title','like','%'.$keyword.'%');
        }

        $re['list'] = $spec->page($pageCurrent,20)->order('spec_id','ASC')->getData();
        $re['count'] = $spec->pageCount;
        $re['total'] = $spec->total;
        
        return $re;
    }

   
    public function onSave(){
        $this->power('shop.setting.spec');
        $spec_id = $this->post('spec_id',$this->check()->type('int'));
        $data['title'] =  $this->post('title',$this->check()->type('string')->required());

        $spec = $this->model('Spec');

        if ($spec_id){
            if ($spec->updateId($spec_id,$data)){

                $data['spec_id'] = $spec_id;
                $this->log('修改规格',$data);

                return true;
            }else{
                throw new \Exception('修改失败');
            }
        }else{
            if ($spec_id = $spec->insert($data)){
                $data['spec_id'] = $spec_id;
                $this->log('添加规格',$data);
                return true;
            }else{
                throw new \Exception('添加失败');
            }
        }

    }

    public function onCold(){
        $this->power('shop.setting.spec');
        $spec_id =  $this->post('id',$this->check()->type('int')->required());
        $cold = $this->post('cold',$this->check()->type('int')->default(0));
        $spec = $this->model('Spec');
        if ($spec->updateId($spec_id,['cold'=>$cold])){
            
            return true;
        }

        throw new \Exception('修改失败');
    }

    public function onDel(){
        $this->power('shop.setting.spec');
        $spec_id = $this->post('spec_id',$this->check()->type('int')->required());

        $spec = $this->model('Spec');

        $data = $spec->getId($spec_id);


        if ($spec->deleteId($spec_id)){
            $this->log('删除规格',$data);
            return true;
        }else{
            throw new \Exception('删除失败');
        }
    }

    public function onItem(){
        $this->power('shop.setting.spec');
        $spec_id = $this->get('spec',$this->check()->type('int')->required());
        
        $data = $this->model('Spec')->getId($spec_id);

        if (!$data){
            throw new \Exception('规格不存在');    
        }

        $this->pageTitle = $data['title'];
        $this->set('spec',$spec_id);
        
        return $this->display();

    }


    public function onItemTree(){
        $this->power('shop.setting.spec');
        $spec_id = $this->post('spec',$this->check()->type('int')->required());
        $specItem = $this->model('SpecItem');
        $specItem->setSpec($spec_id);
        return ['tree'=>$specItem->getTreeData(0)];
    }

    public function onSaveItem(){
        $this->power('shop.setting.spec');
        $item_id  = $this->post('item_id',$this->check()->type('int'));

        $data['title'] = $this->post('title',$this->check()->required());
        $data['spec_id'] = $this->post('spec_id',$this->check()->type('int')->required());
        $data['parent_id']= $this->post('parent_id',$this->check()->type('int')->required());
        $data['type'] = $this->post('type',$this->check()->type('string')->required());

        $data['defaultValue'] = $this->post('defaultValue',$this->check()->type('string'));


        if ($data['type'] =='select'){
            try{
                $data['options'] = json_encode($this->post('options',[]));
            }catch(\Exception $e){
                $data['options'] = '';
            }
        }
        
        $specItem = $this->model('SpecItem');
        $specItem->setSpec($data['spec_id']);
        if ($item_id){
            $oldData = $specItem->getId($item_id);
        
            $childrenList = $specItem->getKeyCollect($oldData['item_id']);
 
            if (in_array($data['parent_id'],$childrenList)){
                throw new \Exception('不能选择子分类作为父分类');
            }
            
            if ($oldData['type'] == 'node' && $data['type'] != 'node'){
                if ($specItem->where('parent_id',$item_id)->count()>0){
                    throw new \Exception('该分类下有子分类，不能修改类型');
                }
            }

            if ($specItem->updateId($item_id,$data)){
                $this->log('修改规格项',$data);
                return true;
            }    
        }else{

            $data['sort'] = $specItem->where('parent_id',$data['parent_id'])->max('sort')+1;

            if ($specItem->insert($data)){
                $this->log('添加规格项',$data);
                return true;
            }
        }

        throw new \Exception('保存失败');
    }


    public function onDelItem(){
        $this->power('shop.setting.spec');
        $item_id  = $this->post('item_id',$this->check()->type('int')->required());
        $spec_id = $this->post('spec_id',$this->check()->type('int')->required());

        $specItem = $this->model('SpecItem');
        $specItem->setSpec($spec_id);


        $oldData = $specItem->getId($item_id);
        
        if ($oldData['type']=='node' && $specItem->where('parent_id',$item_id)->count()>0){
            throw new \Exception('该节点下有子节点，不能删除');
        }


        if ($specItem->deleteId($item_id)){
            $this->log('删除规格项',$oldData);
            return true;
        }

        throw new \Exception('删除失败');
    }

    public function onItemSort(){
        $this->power('shop.setting.spec');
        $ids =  $this->post('list',[]);
        $spec_id = $this->post('spec_id',$this->check()->type('int')->required());

        if (count($ids)<2){
            throw new \Exception('参数错误');
        }

        $specItem = $this->model('SpecItem');
        $specItem->setSpec($spec_id);

        $specItem->switchSort($ids,'sort','asc');

        return true;
    }

}